package com.atguigu.gulimall.product.WebController;

import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.service.CategoryService;
import com.atguigu.gulimall.product.vo.Catelog2Vo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
public class IndexController {

    @Autowired
    CategoryService categoryService;
    /**
     * 访问 首页
     * @param model
     * @return
     */
     @GetMapping({"/","/index.html"})
      public String indexPage(Model model){
         // 获取一级分类
         List<CategoryEntity> categoryEntityList=categoryService.getLevelOne();
         // model 的数据会被放在请求域中
         model.addAttribute("categorys",categoryEntityList);
          return "index";
      }

    /**
     * 当首页 完成加载 就会 自动发送该请求 查询数据库 下所有分类及子分类 的数据
     * 对于  mvc 来说  map 就是json
     * @return
     */
    @ResponseBody
    @GetMapping("index/json/catalog.json")
      public Map<String,List<Catelog2Vo>> getCateLogJson(){
        Map<String,List<Catelog2Vo>>  cateLogJson=  categoryService.getCateLogJson();
         return cateLogJson;
      }


    @ResponseBody
    @GetMapping("/hello")
     public String getHello(){
        return "hello";
     }
}
